package com.zhengdongzulin.zuke.app.main

import androidx.fragment.app.Fragment
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.model.LatLng
import com.flyco.tablayout.listener.CustomTabEntity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.TabEntity
import com.zhengdongzulin.zuke.app.main.activies.ZKActiviesFragment
import com.zhengdongzulin.zuke.app.main.apartment.ZKApartmentFragment
import com.zhengdongzulin.zuke.app.main.home.ZKHomeFragment
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityMainBinding

/**
 * jeff
 * 2021-03-29 11:00:53
 */
class ZKMainActivity : BaseActivity<BaseViewModel, ZkActivityMainBinding>() {

    override fun getLayoutResId(): Int = R.layout.zk_activity_main

    var mFragments: ArrayList<Fragment> = arrayListOf()

    private val mTitles = arrayOf("首页", "活动", "公寓")
    private val mIconSelectIds = intArrayOf(
        R.mipmap.homemenu1s, R.mipmap.homemenu5s, R.mipmap.homemenu2s
    )
    private val mIconUnselectIds = intArrayOf(
        R.mipmap.homemenu1, R.mipmap.homemenu5, R.mipmap.homemenu2
    )
    private val mTabEntities = arrayListOf<CustomTabEntity>()

    override fun initVM(): BaseViewModel = BaseViewModel()

    override fun initView() {
        for (i in mTitles.indices) {
            mTabEntities.add(TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]))
        }
        mFragments.add(ZKHomeFragment.newInstance())
        mFragments.add(ZKActiviesFragment.newInstance())
        mFragments.add(ZKApartmentFragment.newInstance())


        mView.tableLayout.setTabData(mTabEntities, this, R.id.vp_fragment, mFragments)
        (mFragments[0] as ZKHomeFragment).OnMenuClick {
            when (it) {
                "公寓" -> {
                    mView.tableLayout.currentTab = 1
                }
//                "整租", "合租" -> {
//                    mView.tableLayout.currentTab = 2
//                    (mFragments[2] as ZKFindHouseFragment).initLocationPopwinndow()
//                    (mFragments[2] as ZKFindHouseFragment).initPricePopwindow()
//                    (mFragments[2] as ZKFindHouseFragment).initHdirectionPopwindow()
//                    (mFragments[2] as ZKFindHouseFragment).initHouseTypePopwinndow(it)
//                    (mFragments[2] as ZKFindHouseFragment).resetFilter()
//                    (mFragments[2] as ZKFindHouseFragment).getData()
//                }
            }
        }
    }

    override fun initData() {

    }

    override fun initListener() {
        XXPermissions.with(this)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .request(object : OnPermissionCallback {
                override fun onGranted(
                    permissions: MutableList<String>?,
                    all: Boolean
                ) {
                    var locationClient = LocationClient(this@ZKMainActivity)
                    locationClient.apply {
                        registerLocationListener(object : BDAbstractLocationListener() {
                            override fun onReceiveLocation(p0: BDLocation?) {
                                if (p0 != null && p0.latitude != 0.0 && p0.longitude != 0.0)
                                    mShareViewModel.mLatLng.postValue(
                                        LatLng(
                                            p0!!.latitude,
                                            p0!!.longitude
                                        )
                                    )
                            }
                        })
                        locOption = LocationClientOption().apply {
                            locationMode =
                                LocationClientOption.LocationMode.Hight_Accuracy//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
                            setScanSpan(1000)//可选，默认0，即仅定位一次，设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
                            setIsNeedAddress(true)//可选，设置是否需要地址信息，默认不需要
                            setIsNeedLocationDescribe(true)//可选，设置是否需要地址描述
                            setIgnoreKillProcess(true)//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
                            setIsNeedLocationDescribe(true)//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
                            isOpenGps = true//可选，默认false，设置是否开启Gps定位
                        }
                        start()
                    }
                }

                override fun onDenied(
                    permissions: MutableList<String>?,
                    never: Boolean
                ) {
                    if (never) {
                        //toast("被永久拒绝位置授权，请手动授予App权限");
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                                    XXPermissions.startPermissionActivity(MainActivity.this, permissions);
                    } else {
                        //toast("位置权限被拒绝")
                    }
                }

            })
    }

    override fun startObserve() {

    }

}